package l1j.opqlo.NewSystem.Extension;

import java.util.Timer;
import java.util.TimerTask;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_NPCPack;

public class L1TDtest {
	private class MobMove extends TimerTask {

		L1NpcInstance _bugBear;

		MobMove(L1NpcInstance npc) {
			this._bugBear = npc;
		}

		private void begin() {
			final Timer timer = new Timer();
			timer.schedule(this, 1 * 1000);
		}

		private int calcSleepTime(int sleepTime, L1NpcInstance npc) {
			if (npc.getBraveSpeed() == 1) {
				sleepTime -= sleepTime * 0.25;
			}

			return sleepTime;
		}

		@Override
		public void run() {
			int sleepTime = 0;
			while (!this._bugBear.noTarget()) {
				try {
					Thread.sleep(sleepTime);
				} catch (final InterruptedException e) {
					e.printStackTrace();
				}

				this._bugBear.setHeading(L1TDtest.this.cha.targetDirection(
						L1TDtest.this.cha.getX(), L1TDtest.this.cha.getY()));

				while (!this._bugBear.getMap().isPassable(this._bugBear.getX(),
						this._bugBear.getY(), this._bugBear.getHeading())) {
					if (this._bugBear.getMap().isPassable(this._bugBear.getX(),
							this._bugBear.getY(),
							this._bugBear.getHeading() + 1)) {
						this._bugBear
								.setHeading(L1TDtest.this
										.rePressHeading(this._bugBear
												.getHeading() + 1));
						break;
					} else {
						this._bugBear
								.setHeading(L1TDtest.this
										.rePressHeading(this._bugBear
												.getHeading() - 1));
						if (this._bugBear.getMap().isPassable(
								this._bugBear.getX(), this._bugBear.getY(),
								this._bugBear.getHeading())) {
							break;
						}
					}
				}
//				this._bugBear.setDirectionMove(this._bugBear.getHeading());// ヘッジ方向

				// new BugBearRunning(_runnerNumber).
				// インスタンスを生成しないでください　メモリリークが発生します
				sleepTime = this.calcSleepTime(this._bugBear.getPassispeed(),
						this._bugBear);

			}
			// ((L1MonsterInstance)_bugBear).onNpcAI();
			this.cancel();
		}
	}

	private static L1TDtest _instance;

	public static L1TDtest getInstance() {
		if (_instance == null) {
			_instance = new L1TDtest();
		}
		return _instance;
	}

	private MobMove Stage;
	private L1Character cha;

	private int rePressHeading(int heading) {
		if (0 > heading) {// 0未満ならば7
			heading = 7;
		}
		if (7 < heading) {// 7より大きいなら0
			heading = 0;
		}
		return heading;
	}

	/**
	 * 指定されたロケーションに任意のNpcを一匹生成する。
	 *
	 * @param loc
	 *            出現位置
	 * @param npcid
	 *            任意のNpcId
	 * @param heading
	 *            向き
	 * @return L1NpcInstance 戻り値 : 成功=生成したインスタンス 失敗=null
	 */
	private L1NpcInstance spawnOne(final int x, final int y, int npcid,
			int heading) {
		final L1NpcInstance mob = new L1NpcInstance(NpcTable.getInstance()
				.getTemplate(npcid));
		if (mob != null) {

			mob.setNameId(mob.getNameId());
			mob.setId(IdFactory.getInstance().nextId());
			mob.setHeading(heading);
			mob.setX(32868);
			mob.setHomeX(32868);
			mob.setY(32602);
			mob.setHomeY(32602);
			mob.setMap((short) 511);
			mob.setPassispeed(mob.getPassispeed() * 2);
			L1World.getInstance().storeObject(mob);
			L1World.getInstance().addVisibleObject(mob);

			final S_NPCPack s_npcPack = new S_NPCPack(mob);
			for (final L1PcInstance pc : L1World.getInstance()
					.getRecognizePlayer(mob)) {
				pc.addKnownObject(mob);
				mob.addKnownObject(pc);
				pc.sendPackets(s_npcPack);
			}
			// モンスターのＡＩを開始
			mob.onNpcAI();
			mob.turnOnOffLight();
			mob.startChat(L1NpcInstance.CHAT_TIMING_APPEARANCE); // チャット開始
			try {
				Thread.sleep(300);
			} catch (final InterruptedException e) {
				e.printStackTrace();
			}
		}
		return mob;
	}

	public void Start() {

		for (final L1Object obj : L1World.getInstance().getObject()) {
			if (obj.getMapId() == 511) {
				if (obj instanceof L1MonsterInstance) {
					final L1MonsterInstance tower = (L1MonsterInstance) obj;
					if (tower.getNpcId() == 2000207) {
						this.cha = tower;
					}
				}
			}
		}
		for (int i = 0; i < 15; i++) {
			int x = 32868;
			final int y = 32602;
			final L1NpcInstance npc = this.spawnOne(x, y, 45573, 0);
			if (npc != null) {
				this.Stage = new MobMove(npc);
			}
			this.Stage.begin();
			x++;
		}

	}
}
